import { addSave, codeUpdateWorkDrawing, getInfo } from '@/api/code'
import { ref } from 'vue'

/**
 * 创建序列号
 */
export const useCreateCode = () => {
  const createLoading = ref(false)
  const createForm = ref<CodeAPIRequest.Create>({
    saveVoList: [],
  })

  const resetForm = () => {
    createForm.value.saveVoList.length = 0
  }

  const formSave = async () => {
    try {
      createLoading.value = true
      const res = await addSave(createForm.value)
      createLoading.value = false
      return res.data
    } catch (error) {
      createLoading.value = false
      return {
        code: 10000,
        message: '创建序列号失败，请稍后重试',
        data: {},
        success: false,
      }
    }
  }

  return {
    createForm,
    createLoading,
    formSave,
    resetForm,
  }
}

/**
 * 获取序列号详情服务
 */
export const useGetSnCodeDetailService = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 序列号详情
  const detail = ref<SnCodeDetailAPIResponse.SnCodeDetail>({
    activa: false,
    activaTime: '',
    address: '',
    area: '',
    batchCode: '',
    batchId: '',
    brand: '',
    brandId: '',
    buyMarket: '',
    city: '',
    codeDisableStatus: false,
    codeSource: 1,
    contractNo: '',
    createEmp: '',
    createTime: '',
    dateOfProduction: '',
    dl: '',
    firstSelectTime: null,
    goodsCode: '',
    goodsId: '',
    goodsName: '',
    goodsSpecId: '',
    goodsSpecName: '',
    id: '',
    inboundCode: null,
    inboundTime: null,
    lastSelectTime: null,
    model: '',
    network: '',
    outboundCode: null,
    outboundStoreNumber: '',
    outboundTime: null,
    phone: '',
    produceDate: '',
    province: '',
    register: false,
    registerTime: '',
    remark: '',
    saleNetworkName: '',
    saleNetworkNumber: '',
    salePrice: '',
    securityCode: '',
    selectNum: 0,
    sendGoodsNumber: '',
    specialBz: '',
    standard: '',
    standby: 0,
    status: 1,
    storeCode: '',
    storeStatus: 1,
    town: '',
    type: 1,
    updateEmp: '',
    updateTime: '',
    username: '',
    voltage: '',
    warrantyDay: 0,
    warrantyEndTime: '',
    warrantyStartTime: '',
    xlh: '',
    bfFileUrlList: [],
    cadUrlList: [],
    keywordUrlList: [],
  })

  /**
   * 获取序列号详情
   * @param snCodeId - 序列号id
   */
  const getDetail = async (snCodeId: string) => {
    try {
      getDetailLoading.value = true
      const res = await getInfo(snCodeId)
      getDetailLoading.value = false
      detail.value = res.data.data.row
      if (res.data.code === 20000) {
        return ''
      }

      return res.data.message
    } catch (error) {
      getDetailLoading.value = false
      return '获取序列号详情失败，请稍后重试'
    }
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 更新工图服务
 */
export const useCodeUpdateWorkDrawingService = () => {
  const updateLoading = ref(false)
  const updateForm = ref<CodeAPIRequest.UpdateWorkDrawingParam>({
    id: '',
    cadUrlList: [],
    keywordUrlList: [],
    bfFileUrlList: [],
  })

  const resetUpdateForm = () => {
    updateForm.value = {
      id: '',
      cadUrlList: [],
      keywordUrlList: [],
      bfFileUrlList: [],
    }
  }

  const save = async () => {
    try {
      updateLoading.value = true
      const res = await codeUpdateWorkDrawing(updateForm.value)
      updateLoading.value = false
      return res.data
    } catch (error) {
      updateLoading.value = false
    }
  }

  return {
    updateForm,
    updateLoading,
    save,
    resetUpdateForm,
  }
}
